﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using classMeeting.App_Code;

namespace classMeeting.Student
{
    public partial class Declaration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!IsPostBack)
            {
                bindDro();
                bindTable();
            }
        }

        protected void bindDro()
        {
            string college = (string)Session["college"];
            DataBind db = new DataBind();
            db.bindDro(droInstrutor, college, "班级辅导员");
        }

        protected void sub_Click(object sender, EventArgs e)
        {
            if (checkIsSub())
                return;
            checkLogin();
            subData();
        }

        private bool checkIsSub()
        {

            DataSet dt = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[project] WHERE [id] = '" + getId() + "'");
            if (dt.Tables[0].Rows.Count>0) {
                string state = dt.Tables[0].Rows[0]["state"].ToString();
                if (state.Equals("等待班级辅导员审核") || state.Equals("等待学院负责人审核") || state.Equals("等待学校管理员审核") || state.Equals("已通过审核"))
                {
                    Response.Write("<script>alert('您的申请已经提交，无法保存或者重复提交！');</script>");
                    return true;
                }
            }
            return false;
        }

        public String getId()
        {
            string ID = "";
            try
            {
                ID = System.Text.RegularExpressions.Regex.Replace(System.Web.HttpContext.Current.Server.HtmlDecode(Request.QueryString["UrlID"].ToString()), "'", "");
            }
            catch (NullReferenceException e)
            {
                Response.Redirect("Home.aspx");
            }
            return ID;
        }

        protected void bindTable()
        {
            string ID = "";
            try
            {
                ID = System.Text.RegularExpressions.Regex.Replace(System.Web.HttpContext.Current.Server.HtmlDecode(Request.QueryString["UrlID"].ToString()), "'", "");
            }
            catch (NullReferenceException e)
            {
                Response.Redirect("Home.aspx");
            }
            finally
            {
                txtOrganization.Text = (string)Session["college"];
                DataSet dt = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[project] WHERE [id] = '" + ID + "'");
                if (dt.Tables[0].Rows.Count > 0)
                {
                    //droInstrutor.SelectedValue = dt.Tables[0].Rows[0]["hisInstructor"].ToString();
                    droInstrutor.SelectedValue = dt.Tables[0].Rows[0]["hisClassInstructor"].ToString();
                    txtClass.Text = dt.Tables[0].Rows[0]["class"].ToString();
                    txtOrganization.Text = dt.Tables[0].Rows[0]["organization"].ToString();
                    txtMeetingName.Text = dt.Tables[0].Rows[0]["meetingName"].ToString();
                    txtTime.Text = dt.Tables[0].Rows[0]["time"].ToString();
                    txtAddress.Text = dt.Tables[0].Rows[0]["address"].ToString();
                    txtLeader.Text = dt.Tables[0].Rows[0]["leader"].ToString();
                    txtLeaderPhone.Text = dt.Tables[0].Rows[0]["leaderPhone"].ToString();
                    txtTarget.Text = dt.Tables[0].Rows[0]["target"].ToString();
                    txtParticipants.Text = dt.Tables[0].Rows[0]["participants"].ToString();
                    txtAdviser.Text = dt.Tables[0].Rows[0]["adviser"].ToString();
                    txtAdviserPosition.Text = dt.Tables[0].Rows[0]["adviserPosition"].ToString();
                    txtAdviserPhone.Text = dt.Tables[0].Rows[0]["adviserPhone"].ToString();
                    txtGoal.Text = dt.Tables[0].Rows[0]["goal"].ToString();
                    txtDetails.Text = dt.Tables[0].Rows[0]["details"].ToString();
                    txtPlan.Text = dt.Tables[0].Rows[0]["plan"].ToString();
                    txtSafe.Text = dt.Tables[0].Rows[0]["safe"].ToString();
                    txtBudget.Text = dt.Tables[0].Rows[0]["budget"].ToString();
                    txtCharge.Text = dt.Tables[0].Rows[0]["charge"].ToString();
                    txtMonitor.Text = dt.Tables[0].Rows[0]["monitor"].ToString();
                    labStuPriSug.Text = dt.Tables[0].Rows[0]["colInsSug"].ToString() + "<br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + dt.Tables[0].Rows[0]["colInsTime"].ToString();
                    labAdmSug.Text = dt.Tables[0].Rows[0]["admSug"].ToString() + "<br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + dt.Tables[0].Rows[0]["admTime"].ToString();
                    String sqlTheme = dt.Tables[0].Rows[0]["theme"].ToString();
                    String isView = dt.Tables[0].Rows[0]["isView"].ToString();
                    if (sqlTheme.Equals("1"))
                        theme1.Checked = true;
                    else if (sqlTheme.Equals("2"))
                        theme2.Checked = true;
                    else if (sqlTheme.Equals("3"))
                        theme3.Checked = true;
                    else
                    {
                        theme4.Checked = true;
                        txtOTheme.Text = sqlTheme;
                    }

                    if (isView.Equals("是"))
                        view1.Checked = true;
                    else if (isView.Equals("否"))
                        view2.Checked = true;
                }
                else if (ID != "88830180")
                {
                    Response.Redirect("home.aspx");
                }
            }

        }

        protected void checkLogin()
        {
            String name = (String)Session["name"];
            String schoolId = (String)Session["schoolId"];
            CheckLogin check = new CheckLogin(name, schoolId);
            if (!check.isStudent())
            {
                Response.Redirect("http://www.yiban.cn/school/index/id/5000207");
            }
            DataSet dt1 = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[admin] WHERE [schoolId] = '" + schoolId + "'");
            labUser.Text = (string)dt1.Tables[0].Rows[0]["name"];
        }

        protected void subData()
        {
            String organization = txtOrganization.Text, _class = txtClass.Text, meetingName = txtMeetingName.Text, time = txtTime.Text, address = txtAddress.Text, leader = txtLeader.Text, leaderPhone = txtLeaderPhone.Text, target = txtTarget.Text, participants = txtParticipants.Text, adviser = txtAdviser.Text, adviserPosition = txtAdviserPosition.Text, adviserPhone = txtAdviserPhone.Text, goal = txtGoal.Text, details = txtDetails.Text, plan = txtPlan.Text, safe = txtSafe.Text, budget = txtBudget.Text, charge = txtCharge.Text, monitor = txtMonitor.Text;
            String subTime = DateTime.Now.ToLongDateString();
            String instrutor = droInstrutor.SelectedValue.ToString();
            String theme = "";
            String isView = "";
            String submitter = (String)Session["name"];
            String submitterSchoolId = (String)Session["schoolId"];

            if (theme1.Checked)
                theme = "1";
            else if (theme2.Checked)
                theme = "2";
            else if (theme3.Checked)
                theme = "3";
            else if (theme4.Checked)
                theme = txtOTheme.Text;

            if (view1.Checked)
                isView = "是";
            else if (view2.Checked)
                isView = "否";

            if (organization.Equals("")|| _class.Equals("")|| meetingName.Equals("") || time.Equals("") || address.Equals("") || leader.Equals("") || leaderPhone.Equals("") || target.Equals("") || participants.Equals("") || adviser.Equals("") || adviserPosition.Equals("") || adviserPhone.Equals("") || goal.Equals("") || details.Equals("") || instrutor.Equals("请选择") || isView.Equals("") || theme.Equals("") || plan.Equals("") || safe.Equals("") || budget.Equals("") || charge.Equals("") || monitor.Equals(""))
            {
                Response.Write("<Script>alert('请将所有项目填写完毕！');</Script>");
                return;
            }
            int judNum = 0;
            if (getId().Equals("88830180"))
            {
                judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "INSERT INTO classMeeting.dbo.project([organization],[class],[meetingName],[time],[address],[leader],[leaderPhone],[target],[participants],[theme],[isView],[adviser],[adviserPosition],[adviserPhone],[goal],[details],[plan],[safe],[budget],[charge],[monitor],[subTime],[submitter],[state],[submitterSchoolId],[hisClassInstructor]) VALUES ('" + organization + "','" + _class + "','" + meetingName + "','" + time + "','" + address + "','" + leader + "','" + leaderPhone + "','" + target + "','" + participants + "','" + theme + "','" + isView + "','" + adviser + "','" + adviserPosition + "','" + adviserPhone + "','" + goal + "','" + details + "','" + plan + "','" + safe + "','" + budget + "','" + charge + "','" + monitor + "','" + subTime + "','" + submitter + "','等待班级辅导员审核','" + submitterSchoolId + "','" + instrutor + "')");
            }
            else
            {
                judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "UPDATE [classMeeting].[dbo].[project] SET[organization] ='" + organization + "',[class] = '" + _class + "',[meetingName] = '" + meetingName + "',[time] = '" + time + "' ,[address] = '" + address + "',[leader] = '" + leader + "' ,[leaderPhone] ='" + leaderPhone + "',[target] = '" + target + "',[participants] ='" + participants + "',[theme] ='" + theme + "',[isView] = '" + isView + "',[adviser] = '" + adviser + "',[adviserPosition] ='" + adviserPosition + "',[adviserPhone] = '" + adviserPhone + "',[goal] ='" + goal + "',[details] ='" + details + "',[plan] ='" + plan + "',[safe] ='" + safe + "',[budget] ='" + budget + "',[charge] ='" + charge + "',[monitor] ='" + monitor + "',[subTime] ='" + subTime + "',[submitter] = '" + submitter + "',[state] = '等待班级辅导员审核',[submitterSchoolId] ='" + submitterSchoolId + "',[hisClassInstructor] = '" + instrutor + "'WHERE [id]='" + getId() + "' ");
            }
            if (judNum > 0)
            {
                Response.Write("<Script>alert('提交成功，请等待班级辅导员审核！');window.location.href='Home.aspx';</Script>");
                return;
            }
            else
            {
                Response.Write("<Script>alert('提交失败，请重试或联系网站负责人！');</Script>");
                return;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (checkIsSub())
                return;
            String organization = txtOrganization.Text, _class = txtClass.Text, meetingName = txtMeetingName.Text, time = txtTime.Text, address = txtAddress.Text, leader = txtLeader.Text, leaderPhone = txtLeaderPhone.Text, target = txtTarget.Text, participants = txtParticipants.Text, adviser = txtAdviser.Text, adviserPosition = txtAdviserPosition.Text, adviserPhone = txtAdviserPhone.Text, goal = txtGoal.Text, details = txtDetails.Text, plan = txtPlan.Text, safe = txtSafe.Text, budget = txtBudget.Text, charge = txtCharge.Text, monitor = txtMonitor.Text;
            String subTime = DateTime.Now.ToLongDateString();
            String instrutor = droInstrutor.SelectedValue.ToString();
            String theme = "";
            String isView = "";
            String submitter = (String)Session["name"];
            String submitterSchoolId = (String)Session["schoolId"];
            if (theme1.Checked)
                theme = "1";
            else if (theme2.Checked)
                theme = "2";
            else if (theme3.Checked)
                theme = "3";
            else if (theme4.Checked)
                theme = txtOTheme.Text;

            if (view1.Checked)
                isView = "是";
            else if (view2.Checked)
                isView = "否";
            int judNum = 0;

            if (organization.Equals("") || meetingName.Equals("") || time.Equals("") || address.Equals("") || leader.Equals("") || leaderPhone.Equals("") || target.Equals("") || participants.Equals("") || adviser.Equals("") || adviserPosition.Equals("") || adviserPhone.Equals("") || goal.Equals("") || details.Equals("") || instrutor.Equals("请选择") || isView.Equals("") || theme.Equals("") || plan.Equals("") || safe.Equals("") || budget.Equals("") || charge.Equals("") || monitor.Equals(""))
            {
                Response.Write("<Script>alert('请将所有项目填写完毕！');</Script>");
                return;
            }

            if (getId().Equals("88830180"))
            {
                judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "INSERT INTO classMeeting.dbo.project([organization],[class],[meetingName],[time],[address],[leader],[leaderPhone],[target],[participants],[theme],[isView],[adviser],[adviserPosition],[adviserPhone],[goal],[details],[plan],[safe],[budget],[charge],[monitor],[subTime],[submitter],[state],[submitterSchoolId],[hisClassInstructor]) VALUES ('" + organization + "','" + _class + "','" + meetingName + "','" + time + "','" + address + "','" + leader + "','" + leaderPhone + "','" + target + "','" + participants + "','" + theme + "','" + isView + "','" + adviser + "','" + adviserPosition + "','" + adviserPhone + "','" + goal + "','" + details + "','" + plan + "','" + safe + "','" + budget + "','" + charge + "','" + monitor + "','" + subTime + "','" + submitter + "','等待提交','" + submitterSchoolId + "','" + instrutor + "')");
            }
            else
            {
                judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "UPDATE [classMeeting].[dbo].[project] SET[organization] ='" + organization + "',[class] = '" + _class + "',[meetingName] = '" + meetingName + "',[time] = '" + time + "' ,[address] = '" + address + "',[leader] = '" + leader + "' ,[leaderPhone] ='" + leaderPhone + "',[target] = '" + target + "',[participants] ='" + participants + "',[theme] ='" + theme + "',[isView] ='" + isView + "',[adviser] = '" + adviser + "',[adviserPosition] ='" + adviserPosition + "',[adviserPhone] = '" + adviserPhone + "',[goal] ='" + goal + "',[details] ='" + details + "',[plan] ='" + plan + "',[safe] ='" + safe + "',[budget] ='" + budget + "',[charge] ='" + charge + "',[monitor] ='" + monitor + "',[subTime] ='" + subTime + "',[submitter] = '" + submitter + "',[state] = '等待提交',[submitterSchoolId] ='" + submitterSchoolId + "',[hisClassInstructor] = '" + instrutor + "'WHERE [id]='" + getId() + "' ");
            }
            if (judNum > 0)
            {
                Response.Write("<Script>alert('保存成功！');window.location.href='Home.aspx';</Script>");
                return;
            }
            else
            {
                Response.Write("<Script>alert('保存失败，请重试或联系网站负责人！');</Script>");
                return;
            }
        }

        protected void libExit_Click(object sender, EventArgs e)
        {
            Session["name"] = null;
            Session["schoolId"] = null;
            Session["college"] = null;
            Response.Redirect("http://www.yiban.cn/school/index/id/5000207");
        }
    }
}